from django.db import models

from common.models import CreateUpdateModel


class InitTaskList(CreateUpdateModel):
    """装机初始化任务列表"""
    ticketSn = models.CharField(max_length=64, null=True, blank=True, verbose_name="工单编号")
    ticketId = models.IntegerField(null=True, blank=True, verbose_name="工单ID")
    systemSN = models.CharField(max_length=64, null=True, blank=True, verbose_name="资产SN")
    idc = models.CharField(max_length=64, null=True, blank=True, verbose_name="所属机房")
    rackBuilding = models.CharField(max_length=64, null=True, verbose_name="所属楼栋")
    businessIp = models.CharField(max_length=64, null=True, blank=True, verbose_name="业务地址")
    netmask = models.IntegerField(null=True, verbose_name="业务掩码")
    gateway = models.CharField(max_length=64, null=True, blank=True, verbose_name="业务网关")
    status = models.CharField(max_length=64, null=True, blank=True, verbose_name="状态")
    flowInfo = models.CharField(max_length=512, null=True, blank=True, verbose_name="流程")
    completeTime = models.DateTimeField(null=True, blank=True, verbose_name="完成时间")

    class Meta:
        managed = True
        db_table = 'InitTaskList'
        ordering = ['-create_time']


class TicketRecord(models.Model):
    """
    工单记录
    """
    title = models.CharField(u'标题', max_length=500, blank=True, default='', help_text="工单的标题")
    workflow_id = models.IntegerField('关联的流程id', help_text='与workflow.Workflow流程关联')
    sn = models.CharField(u'流水号', max_length=25, help_text="工单的流水号")
    state_id = models.IntegerField('当前状态', help_text='与workflow.State关联')
    parent_ticket_id = models.IntegerField('父工单id', default=0, help_text='与ticket.TicketRecord关联')
    parent_ticket_state_id = models.IntegerField('对应父工单状态id', default=0, help_text='与workflow.State关联,子工单是关联到父工单的某个状态下的')
    participant_type_id = models.IntegerField('当前处理人类型', default=0, help_text='0.无处理人,1.个人,2.多人,3.部门,4.角色')
    participant = models.CharField('当前处理人', max_length=1000, default='', blank=True, help_text='可以为空(无处理人的情况，如结束状态)、username\多个username(以,隔开)\部门id\角色id\脚本文件名等')
    relation = models.CharField('工单关联人', max_length=1000, default='', blank=True, help_text='工单流转过程中将保存所有相关的人(包括创建人、曾经的待处理人)，用于查询')
    in_add_node = models.BooleanField('加签状态中', default=False, help_text='是否处于加签状态下')
    add_node_man = models.CharField('加签人', max_length=50, default='', blank=True, help_text='加签操作的人，工单当前处理人处理完成后会回到该处理人，当处于加签状态下才有效')
    script_run_last_result = models.BooleanField(u'脚本最后一次执行结果', default=True)
    act_state_id = models.IntegerField('进行状态', default=1, help_text='当前工单的进行状态,详见service.constant_service中定义')
    multi_all_person = models.CharField('全部处理的结果', max_length=1000, default='{}', blank=True, help_text='需要当前状态处理人全部处理时实际的处理结果，json格式')
    remark = models.TextField('备注', default='', blank=True, help_text="工单的备注")
    computer_room = models.CharField('机房', max_length=64, default='', blank=True, help_text="工单关联的机房")
    server_nums = models.IntegerField('服务器数量', default=0, blank=True, help_text="工单关联的服务器数量")
    distribution_time = models.DateTimeField(null=True, blank=True, verbose_name="工单派发时间")
    sla_deadline = models.DateTimeField(null=True, blank=True, verbose_name="sla截止日期")
    duration_consumed = models.IntegerField(null=True, blank=True, verbose_name="耗费时长(秒)")
    creator = models.CharField('创建人', max_length=50)
    gmt_created = models.DateTimeField('创建时间', auto_now_add=True)
    gmt_modified = models.DateTimeField('更新时间', auto_now=True)
    is_deleted = models.BooleanField('已删除', default=False)

    class Meta:
        managed = False
        db_table = 'ticket_ticketrecord'
        ordering = ['-gmt_created']


class OSSystemInstallHaveSN(models.Model):  # OS配装-有sn表
    ticket_id = models.IntegerField(default=0, verbose_name='工单id')
    ticket_sn = models.CharField(max_length=100, default='', verbose_name='工单sn')
    sn = models.CharField(max_length=100, default='', verbose_name='明细sn')
    material_type = models.CharField(max_length=100, default='', verbose_name='设备类型')
    manufacturer = models.CharField(max_length=100, default='', verbose_name='厂商')
    model_specifications = models.CharField(max_length=100, default='', verbose_name='型号')

    source_computer_room = models.CharField(max_length=100, default='', verbose_name='所在机房')
    source_building = models.CharField(max_length=100, default='', verbose_name='所在楼栋')
    source_cabinet = models.CharField(max_length=100, default='', verbose_name='所在机柜')
    source_frame_info = models.CharField(max_length=100, default='', verbose_name='所在机位')

    is_auth_scene_shutdown = models.CharField(max_length=20, default='', verbose_name='授权关机 是/否')
    raid_config = models.CharField(max_length=100, default='', verbose_name='RAID配置')
    system_version = models.CharField(max_length=100, default='', verbose_name='系统版本')
    internal_ip_config = models.CharField(max_length=100, default='', verbose_name='内网IP配置')
    netmask = models.CharField(max_length=100, default='', verbose_name='掩码')
    gateway = models.CharField(max_length=100, default='', verbose_name='网关')
    drive_config = models.CharField(max_length=100, default='', verbose_name='驱动配置')
    out_of_band_ip = models.CharField(max_length=100, default='', verbose_name='带外IP')
    system_partition = models.CharField(max_length=100, default='', verbose_name='系统分区')
    resource_assembly = models.CharField(max_length=500, default='', verbose_name='资源装配')

    external_ip_connect_check = models.CharField(max_length=20, default='', verbose_name='带外IP联通检查 正常/不正常')
    raid_config_check = models.CharField(max_length=20, default='', verbose_name='Raid配置检查 正常/不正常')
    system_version_check = models.CharField(max_length=20, default='', verbose_name='系统版本检查 正常/不正常')
    internal_ip_connect_check = models.CharField(max_length=20, default='', verbose_name='内网IP联通检查 正常/不正常')
    server_init_check = models.CharField(max_length=20, default='', verbose_name='服务器初始化检查 正常/不正常')
    system_partition_check = models.CharField(max_length=20, default='', verbose_name='系统分区检查 正常/异常')

    sn_info = models.TextField(default='{}', verbose_name='cmdb明细详情')
    gmt_created = models.DateTimeField('创建时间', auto_now_add=True)
    gmt_modified = models.DateTimeField('更新时间', auto_now=True)
    is_deleted = models.BooleanField('已删除', default=False)

    class Meta:
        managed = False
        db_table = 'OSSystemInstall_HaveSN'
        ordering = ['-gmt_created']


class TaskRecord(models.Model):
    """初始化任务流程日志"""
    taskType = models.CharField(max_length=64, null=True, blank=True, verbose_name="任务类型")
    taskId = models.IntegerField(null=True, verbose_name="任务ID")
    nodeName = models.CharField(max_length=64, null=True, blank=True, verbose_name="节点名称")
    operationUserAccount = models.CharField(max_length=64, null=True, blank=True, verbose_name="操作人域账号")
    operationCnName = models.CharField(max_length=64, null=True, blank=True, verbose_name="操作人中文名")
    status = models.CharField(max_length=64, null=True, blank=True, verbose_name="状态")
    exceptionItem = models.CharField(max_length=128, null=True, blank=True, verbose_name="异常项")
    createTime = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updateTime = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        managed = True
        db_table = 'TaskRecord'
        ordering = ['-createTime']


class InitTaskConfig(CreateUpdateModel):
    """初始化任务配置"""
    username = models.CharField(max_length=64, null=True, blank=True, verbose_name="默认用户名")
    password = models.CharField(max_length=64, null=True, blank=True, verbose_name="默认密码")
    initScriptPath = models.CharField(max_length=128, null=True, blank=True, verbose_name="Init脚本路径")
    bondScriptPath = models.CharField(max_length=128, null=True, blank=True, verbose_name="Bond脚本路径")
    initLogPath = models.CharField(max_length=128, null=True, blank=True, verbose_name="Init日志路径")
    bondLogPath = models.CharField(max_length=128, null=True, blank=True, verbose_name="Bond日志路径")

    class Meta:
        managed = True
        db_table = 'InitTaskConfig'


class OSInstallTaskList(CreateUpdateModel):
    """装机任务列表"""
    ticketSn = models.CharField(max_length=64, null=True, blank=True, verbose_name="工单编号")
    ticketId = models.IntegerField(null=True, blank=True, verbose_name="工单ID")
    systemSN = models.CharField(max_length=64, null=True, blank=True, verbose_name="资产SN")
    mac = models.CharField(max_length=128, null=True, blank=True, verbose_name="MAC地址")
    idc = models.CharField(max_length=64, null=True, blank=True, verbose_name="所属机房")
    rackBuilding = models.CharField(max_length=64, null=True, verbose_name="所属楼栋")
    netEnv = models.CharField(max_length=64, null=True, verbose_name="网络环境")
    dhcpIp = models.CharField(max_length=64, null=True, blank=True, verbose_name="dhcp地址")
    ipmi = models.CharField(max_length=64, null=True, blank=True, verbose_name="带外地址")
    businessIp = models.CharField(max_length=64, null=True, blank=True, verbose_name="业务地址")
    netmask = models.IntegerField(null=True, verbose_name="业务掩码")
    gateway = models.CharField(max_length=64, null=True, blank=True, verbose_name="业务网关")
    bond = models.CharField(max_length=64, null=True, blank=True, verbose_name="Bond模式")
    osVersion = models.CharField(max_length=64, null=True, blank=True, verbose_name="系统版本")
    status = models.CharField(max_length=64, null=True, blank=True, verbose_name="状态")
    flowInfo = models.CharField(max_length=512, null=True, blank=True, verbose_name="流程")
    completeTime = models.DateTimeField(null=True, blank=True, verbose_name="完成时间")

    class Meta:
        managed = True
        db_table = 'OSInstallTaskList'
        ordering = ['-create_time']


class OSInstallTaskList(CreateUpdateModel):
    """装机任务列表"""
    ticketSn = models.CharField(max_length=64, null=True, blank=True, verbose_name="工单编号")
    ticketId = models.IntegerField(null=True, blank=True, verbose_name="工单ID")
    systemSN = models.CharField(max_length=64, null=True, blank=True, verbose_name="资产SN")
    mac = models.CharField(max_length=128, null=True, blank=True, verbose_name="MAC地址")
    idc = models.CharField(max_length=64, null=True, blank=True, verbose_name="所属机房")
    rackBuilding = models.CharField(max_length=64, null=True, verbose_name="所属楼栋")
    netEnv = models.CharField(max_length=64, null=True, verbose_name="网络环境")
    dhcpIp = models.CharField(max_length=64, null=True, blank=True, verbose_name="dhcp地址")
    ipmi = models.CharField(max_length=64, null=True, blank=True, verbose_name="带外地址")
    businessIp = models.CharField(max_length=64, null=True, blank=True, verbose_name="业务地址")
    netmask = models.IntegerField(null=True, verbose_name="业务掩码")
    gateway = models.CharField(max_length=64, null=True, blank=True, verbose_name="业务网关")
    bond = models.CharField(max_length=64, null=True, blank=True, verbose_name="Bond模式")
    osVersion = models.CharField(max_length=64, null=True, blank=True, verbose_name="系统版本")
    status = models.CharField(max_length=64, null=True, blank=True, verbose_name="状态")
    flowInfo = models.CharField(max_length=512, null=True, blank=True, verbose_name="流程")
    completeTime = models.DateTimeField(null=True, blank=True, verbose_name="完成时间")

    class Meta:
        managed = True
        db_table = 'OSInstallTaskList'
        ordering = ['-create_time']
# Create your models here.